<?php
/**
 * Config reader for arrays in files
 * @author Kirill Egorov 2010
 * @package Config
 * @subpackage File
 */
class Config_File_Array extends Config_File
{
    /**
     * (non-PHPdoc)
     * @see library/Config/File#_readFile($data)
     */
    protected function _readFile($name){
    	if(!file_exists($name))
    		throw new Exception('Cant find '.$name);
  		
        return include $name;
    }
    /**
     * (non-PHPdoc)
     * @see Config_File::save()
     */
    public function save()
    {
        if(!is_writable($this->_name))
            return false;

        if(file_put_contents($this->_name, '<?php return ' . var_export($this->_data , true).'; ')!==false){
           $cache = Config::getCacheCore();
           if($cache)
              $cache->save($this->_data, md5($this->_name),array('config' , 'config_file'));
                     
           return true;     
         }   
         return false; 
    }
	/**
     * Create config
     * @param string $file - path to config
	 * @return boolean - success flag  
     */
    static public function create($file)
    {
    	if(File::getExt($file)!=='.php')
    		throw new Exception('Invalid file name');
    	
    	if(file_put_contents($file, '<?php return array();')!==false)
    	    return true;

    	return false;    
    }
}